/**********************************************************************
 * Project : WoWGuildRoster (http://www.codeplex.com/WowGuildRoster/)
 * Module  : DotNetWarcraft.WoWGuildRoster.UI.DNN.WGR_ControlPanel
 **********************************************************************
 * 
 * 
 * 
 **********************************************************************
 * License : All code is subject to licensing terms per the Microsoft
 *   Permissive License (Ms-PL) v1.1. For full license information,
 *   refer to the packaged license.txt file.
 **********************************************************************
 * Revision History:
 * -----------------
 * 2007-08-26 -- Cliff Simpkins: Initial Version
 * 2007-09-02 -- Cliff Simpkins: Added the rank labels logic
 * 2007-09-16 -- Cliff Simpkins: Added logic to support loads & errors
 **********************************************************************
 */
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Exceptions;

using WGR_Business = DotNetWarcraft.WoWGuildRoster.Business;

namespace DotNetWarcraft.WoWGuildRoster.UI.DNN {
  public partial class WGR_ControlPanel : PortalModuleBase {

    protected const String WGR_CP_VIEW_ROSTERS = "cp_rosters";
    protected const String WGR_CP_VIEW_ERRORS = "cp_errors";
    protected const String WGR_CP_VIEW_LOADS = "cp_loads";
    protected const String WGR_CP_VIEW_SERVICE = "cp_service";
    protected const String WGR_CP_VIEW_DEFAULT = WGR_CP_VIEW_ROSTERS;

    #region "Member Variables"
    #endregion
    #region "Protected Properties"
    protected String WgrCpViewType {
      get {
        return lblViewType.Text;
      }
      set {
        lblViewType.Text = value;
      }
    }

    protected int RosterID {
      get {
        try {
          int _rosterID;
          if (this.Request.QueryString["RosterID"] == null || this.Request.QueryString["RosterID"].ToString() == "") {
            if (lblRosterID.Text.Trim() != "") {
              _rosterID = int.Parse(lblRosterID.Text);
            } else
              _rosterID = -1;
          } else {
            _rosterID = int.Parse(this.Request.QueryString["RosterID"].ToString());
          }
          return _rosterID;
        } catch {
          return -1;
        }
      }
    }
    protected String RosterName {
      get {
        try {
          return this.drpRosterName.SelectedValue.ToString();
        } catch {
          return "";
        }
      }
      set {
        if (this.drpRosterName.SelectedItem != null) {
          try {
            ListItem _li = this.drpRosterName.Items.FindByValue(value);

            if (_li != null) {
              this.drpRosterName.SelectedItem.Selected = false;
              _li.Selected = true;
            }
          } catch {
          }
        }
      }
    }
    protected String RosterRegion_String {
      get {
        if (drpRegionName.Items.Count < 1) {
          LoadDropDown_Regions();
        }
        return drpRegionName.SelectedValue;
      }
      set {
        if (drpRegionName.Items.Count < 1) {
          LoadDropDown_Regions();
        }

        drpRegionName.ClearSelection();
        try {
          if (value.ToString().Trim() == "") {
            drpRegionName.Items.FindByValue(WGR_Business.WGRConstants.DEFAULT_REGION_NAME).Selected = true;
          } else {
            drpRegionName.Items.FindByValue(value).Selected = true;
          }
        } catch (Exception) {
          drpRegionName.Items.FindByValue(WGR_Business.WGRConstants.DEFAULT_REGION_NAME).Selected = true;
        }
      }
    }
    protected WGR_Business.WarcraftRegions RosterRegion {
      get {
        return WGR_Business.WGRConstants.WowRegion_Enum(this.RosterRegion_String);
      }
      set {
        this.RosterRegion_String = WGR_Business.WGRConstants.WowRegion_String(value);
      }
    }
  #endregion
    #region "Protected Methods - Control Visibility"
    protected void SetVisibility() {
      pnlErrorHeader.Visible = false;
      pnlErrorInfo.Visible = false;
      pnlLoadHeader.Visible = false;
      pnlLoadInfo.Visible = false;
      pnlRosterHeader.Visible = false;
      pnlRosterInfo.Visible = false;
      pnlRosterSummary.Visible = false;
      pnlServiceInfo.Visible = false;

      switch (this.WgrCpViewType) {
        case WGR_CP_VIEW_ERRORS:
          pnlErrorHeader.Visible = true;
          pnlErrorInfo.Visible = true;
          this.LoadErrorData();
          break;
        case WGR_CP_VIEW_LOADS:
          pnlLoadHeader.Visible = true;
          pnlLoadInfo.Visible = true;
          this.LoadDataLoadData();
          break;
        case WGR_CP_VIEW_ROSTERS:
          this.SetVisibility_Rosters();
          break;
        case WGR_CP_VIEW_SERVICE:
          pnlServiceInfo.Visible = true;
          break;
      }
    }
    protected void SetVisibility_Rosters() {
      pnlRosterHeader.Visible = true;

      int _rosterID = this.RosterID;

      if (_rosterID > 0) {
        pnlRosterInfo.Visible = true;
        pnlRosterSummary.Visible = false;
        lbAddRoster.Visible = true;
      } else if (_rosterID == 0) {
        pnlRosterInfo.Visible = true;
        pnlRosterSummary.Visible = false;
        lbAddRoster.Visible = false;
      } else {
        pnlRosterInfo.Visible = false;
        pnlRosterSummary.Visible = true;
        lbAddRoster.Visible = true;
      }

      lbDelete.Visible = (_rosterID > 0);
      lbSave.Visible = (_rosterID >= 0);
      lbCancel.Visible = (_rosterID >= 0);
      drpRosterName.Visible = (_rosterID != 0);

    }
  #endregion
    #region "Protected Methods - Data Load/Save"
    protected void LoadDropDown_Regions() {
      drpRegionName.Items.Clear();

      ListItem _liRegionUS = new ListItem("United States",
        WGR_Business.WGRConstants.WowRegion_String(WGR_Business.WarcraftRegions.RegionUS));
      ListItem _liRegionEurope = new ListItem("Europe",
        WGR_Business.WGRConstants.WowRegion_String(WGR_Business.WarcraftRegions.RegionEurope));

      drpRegionName.Items.Add(_liRegionUS);
      drpRegionName.Items.Add(_liRegionEurope);
    }
    protected void LoadDropDown_Rosters() {
      List<WGR_Business.GuildRoster> _rosters = WGR_Business.DnnGuildRosterController.GetRosters();

      drpRosterName.Items.Clear();
      ListItem _liNone = new ListItem("None Selected", "-1");
      drpRosterName.Items.Add(_liNone);

      if (_rosters.Count > 0) {
        lblNoRoster.Visible = false;
        drpRosterName.Visible = true;

        foreach (WGR_Business.GuildRoster _roster in _rosters) {
          ListItem _li = new ListItem(_roster.Guild_Name + " (" + _roster.Guild_Server + ")", _roster.Roster_ID.ToString());
          drpRosterName.Items.Add(_li);
        }

        if (this.RosterID > 0) {
          this.RosterName = this.RosterID.ToString();
        }
      } else {
        drpRosterName.Visible = false;
        //lblNoRoster.Visible = true;
      }

    }

    protected void LoadRosterData() {
      if (this.RosterID > 0) {
        WGR_Business.GuildRoster _roster = WGR_Business.DnnGuildRosterController.GetRoster(this.RosterID);
        this.RosterData_LoadRoster(ref _roster);
      } else if (this.RosterID == 0) {
        //The Roster is 0 (meaning 'let's add a new one!'), so we populate the text boxes with default values
        this.txtGuildName.Text = WGR_Business.WGRConstants.DEFAULT_GUILD_NAME;
        this.txtServerName.Text = WGR_Business.WGRConstants.DEFAULT_SERVER_NAME;
        this.drpRegionName.SelectedItem.Selected = false;
        this.drpRegionName.Items[0].Selected = true;

        this.txtGuildRank0.Text = WGR_Business.WGRConstants.DEFAULT_GUILD_RANK_0;
        this.txtGuildRank1.Text = WGR_Business.WGRConstants.DEFAULT_GUILD_RANK_1;
        this.txtGuildRank2.Text = WGR_Business.WGRConstants.DEFAULT_GUILD_RANK_2;
        this.txtGuildRank3.Text = WGR_Business.WGRConstants.DEFAULT_GUILD_RANK_3;
        this.txtGuildRank4.Text = WGR_Business.WGRConstants.DEFAULT_GUILD_RANK_4;
        this.txtGuildRank5.Text = WGR_Business.WGRConstants.DEFAULT_GUILD_RANK_5;
        this.txtGuildRank6.Text = WGR_Business.WGRConstants.DEFAULT_GUILD_RANK_6;
        this.txtGuildRank7.Text = WGR_Business.WGRConstants.DEFAULT_GUILD_RANK_7;
        this.txtGuildRank8.Text = WGR_Business.WGRConstants.DEFAULT_GUILD_RANK_8;
        this.txtGuildRank9.Text = WGR_Business.WGRConstants.DEFAULT_GUILD_RANK_9;
        this.txtGuildRank10.Text = WGR_Business.WGRConstants.DEFAULT_GUILD_RANK_10;
      }
    }

    protected void RosterData_LoadRoster(ref WGR_Business.GuildRoster rosterToLoad) {
      this.txtGuildName.Text = rosterToLoad.Guild_Name;
      this.txtServerName.Text = rosterToLoad.Guild_Server;
      this.RosterRegion = rosterToLoad.Guild_Region_Enum;

      this.txtGuildRank0.Text = rosterToLoad.GuildRank0;
      this.txtGuildRank1.Text = rosterToLoad.GuildRank1;
      this.txtGuildRank2.Text = rosterToLoad.GuildRank2;
      this.txtGuildRank3.Text = rosterToLoad.GuildRank3;
      this.txtGuildRank4.Text = rosterToLoad.GuildRank4;
      this.txtGuildRank5.Text = rosterToLoad.GuildRank5;
      this.txtGuildRank6.Text = rosterToLoad.GuildRank6;
      this.txtGuildRank7.Text = rosterToLoad.GuildRank7;
      this.txtGuildRank8.Text = rosterToLoad.GuildRank8;
      this.txtGuildRank9.Text = rosterToLoad.GuildRank9;
      this.txtGuildRank10.Text = rosterToLoad.GuildRank10;
    }

    protected void RosterData_SaveRoster(ref WGR_Business.GuildRoster rosterToSave) {
      rosterToSave.Guild_Name = this.txtGuildName.Text;
      rosterToSave.Guild_Server = this.txtServerName.Text;
      rosterToSave.Guild_Region_Enum = this.RosterRegion;

      rosterToSave.GuildRank0 = this.txtGuildRank0.Text;
      rosterToSave.GuildRank1 = this.txtGuildRank1.Text;
      rosterToSave.GuildRank2 = this.txtGuildRank2.Text;
      rosterToSave.GuildRank3 = this.txtGuildRank3.Text;
      rosterToSave.GuildRank4 = this.txtGuildRank4.Text;
      rosterToSave.GuildRank5 = this.txtGuildRank5.Text;
      rosterToSave.GuildRank6 = this.txtGuildRank6.Text;
      rosterToSave.GuildRank7 = this.txtGuildRank7.Text;
      rosterToSave.GuildRank8 = this.txtGuildRank8.Text;
      rosterToSave.GuildRank9 = this.txtGuildRank9.Text;
      rosterToSave.GuildRank10 = this.txtGuildRank10.Text;
    }


    protected void LoadErrorData() {

      List<WGR_Business.GuildError> _errors = WGR_Business.DnnGuildRosterController.GetErrors();

      this.lblErrorInfo_CountValue.Text = _errors.Count.ToString();

      dgErrorList.DataSource = _errors;
      dgErrorList.DataBind();
      dgErrorList.Visible = (_errors.Count > 0);
      lbClearAllErrors.Visible = (_errors.Count > 0);

    }
    protected void LoadDataLoadData() {

      List<WGR_Business.DataLoad> _loads = WGR_Business.DnnGuildRosterController.GetDataLoads();

      this.lblLoadInfo_CountValue.Text = _loads.Count.ToString();

      dgLoadList.DataSource = _loads;
      dgLoadList.DataBind();
      dgLoadList.Visible = (_loads.Count > 0);
      lbClearAllLoads.Visible = (_loads.Count > 0);

    }


    protected void ResetPage() {
      drpRosterName.Items.Clear();
      this.LoadDropDown_Rosters();

      lblRosterID.Text = "-1";
      if (drpRosterName.Items.Count > 0) {
        drpRosterName.SelectedItem.Selected = false;
        drpRosterName.Items.FindByValue("-1").Selected = true;
      }
    }
    protected void ReloadPage() {
      this.LoadRosterData();
      this.SetVisibility();
    }
  #endregion

    protected void Page_Load(object sender, EventArgs e) {
      try {
        this.hlDNW_WGR_site.Text = "WoW Guild Roster v" + WGR_Business.DnnGuildRosterController.VersionNumber;
        this.hlDNW_WGR_site.NavigateUrl = this.EditUrl("About");

        if (DotNetNuke.Framework.AJAX.IsInstalled()) {
          DotNetNuke.Framework.AJAX.RegisterScriptManager();
        }

        if (!Page.IsPostBack) {
          if (this.WgrCpViewType == "")
            this.WgrCpViewType = WGR_CP_VIEW_DEFAULT;

          this.LoadDropDown_Regions();
          this.LoadDropDown_Rosters();
        }

        this.SetVisibility();
      } catch (Exception exc) {
        throw exc;
      }

    }

    #region "Page Events - Control Post Backs"
    protected void drpRosterName_SelectedIndexChanged(object sender, EventArgs e) {

      //System.Web.HttpRequest _req = this.Request;
      //bool _processed = false;
      //String _newUrl = this.Request.Url.AbsoluteUri;
      //_newUrl += "?";

      //for (int loop = 0; loop < this.Request.QueryString.Count; loop++) {
      //  if (loop > 0 && Server.HtmlEncode(this.Request.QueryString.GetKey(loop)).Trim() != "") {
      //    _newUrl += "&";
      //  }
      //  _newUrl += Server.HtmlEncode(this.Request.QueryString.GetKey(loop));
      //  _newUrl += "=";

      //  if (Server.HtmlEncode(this.Request.QueryString.GetKey(loop)).Trim() == "RosterID") {
      //    _newUrl += drpRosterName.SelectedValue.ToString();
      //  } else {
      //    _newUrl += Server.HtmlEncode(this.Request.QueryString[loop]);
      //  }
      //}

      //if (!_processed) {
      //  _newUrl += "RosterID=" + drpRosterName.SelectedValue.ToString();
      //}

      lblRosterID.Text = drpRosterName.SelectedValue.ToString();
      this.LoadRosterData();
      this.SetVisibility();

    }
    #endregion

    #region "Page Events - Link Buttons - Page"
    protected void lbRosters_Click(object sender, EventArgs e) {
      this.WgrCpViewType = WGR_CP_VIEW_ROSTERS;
      this.ResetPage();
      this.SetVisibility();
    }

    protected void lbLoads_Click(object sender, EventArgs e) {
      this.WgrCpViewType = WGR_CP_VIEW_LOADS;
      this.SetVisibility();
    }

    protected void lbErrors_Click(object sender, EventArgs e) {
      this.WgrCpViewType = WGR_CP_VIEW_ERRORS;
      this.SetVisibility();
    }

    protected void lbArmory_Click(object sender, EventArgs e) {
      this.WgrCpViewType = WGR_CP_VIEW_SERVICE;
      this.SetVisibility();

      //DotNetNuke.Services.Scheduling.SchedulingProvider _prov = DotNetNuke.Services.Scheduling.SchedulingProvider(;
      //ArrayList _list = _prov.GetSchedule();
      //foreach (DotNetNuke.Services.Scheduling.ScheduleItem _item in _list) {
      //  lblServiceList.Text += "<br>" + _item.TypeFullName + " (" + _item.ScheduleID + ")";
      //}
    }

    protected void lbBack_Click(object sender, EventArgs e) {
      try {
        this.Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(this.TabId), true);
      } catch (Exception exc) { //Module failed to load
        Exceptions.ProcessModuleLoadException(this, exc);
      }

    }

    protected void lbClearAllErrors_Click(object sender, EventArgs e) {
      try {
        WGR_Business.DnnGuildRosterController.DeleteErrors();
        this.LoadErrorData();
      } catch (Exception exc) { //Module failed to load
        Exceptions.ProcessModuleLoadException(this, exc);
      }

    }

    protected void lbClearAllLoads_Click(object sender, EventArgs e) {
      try {
        WGR_Business.DnnGuildRosterController.DeleteDataLoads();
        this.LoadDataLoadData();
      } catch (Exception exc) { //Module failed to load
        Exceptions.ProcessModuleLoadException(this, exc);
      }
    }

    #endregion

    #region "Page Events - Link Buttons - Rosters"
    protected void lbSave_Click(object sender, EventArgs e) {

      WGR_Business.GuildRoster _roster;

      if (this.RosterID > 0) {
        _roster = WGR_Business.DnnGuildRosterController.GetRoster(this.RosterID);
      } else {
        _roster = new WGR_Business.GuildRoster();
      }
      this.RosterData_SaveRoster(ref _roster);

      if (_roster.Roster_ID > 0) {
        WGR_Business.DnnGuildRosterController.UpdateRoster(_roster);
      } else {
        WGR_Business.DnnGuildRosterController.AddRoster(_roster);
        this.LoadDropDown_Rosters();
      }

      this.ResetPage();
      this.ReloadPage();
    }

    protected void lbDelete_Click(object sender, EventArgs e) {
      if (this.RosterID > 0) {
        WGR_Business.DnnGuildRosterController.DeleteRoster(this.RosterID);
      }
      this.ResetPage();
      this.ReloadPage();
    }

    protected void lbCancel_Click(object sender, EventArgs e) {
      if (this.RosterID == 0) {
        this.ResetPage();
      }
      this.ReloadPage();
    }

    protected void lbAddRoster_Click(object sender, EventArgs e) {
      lblRosterID.Text = "0";
      this.ReloadPage();
    }

    protected void lbForceReload_Click(object sender, EventArgs e) {

      WGR_Business.GuildRoster _roster;

      if (this.RosterID > 0) {
        WGR_Business.DnnGuildRosterController.ProcessGuildEntries_MarkForReload(this.RosterID);
      }
      
      this.ResetPage();
      this.ReloadPage();
    }
    #endregion


  }
}